package net.zabuchy.loginHelper.action;


import java.util.Date;

import net.zabuchy.authenticationHelper.AuthenticationHelper;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.iteration.bo.IterationBo;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private static final Logger LOGGER = Logger.getLogger(LoginAction.class.getName());
	 
    @Autowired
    private SessionHelper sessionBean;
    
    @Autowired
    private AuthenticationHelper authenticationHelper;
    
    @Autowired
    private UserBo userBo;
    
    @Autowired
    private IterationBo iterationBo;

    
	public String login() {
		
		LOGGER.debug("LoginAction.login()");
		
		User user = userBo.findByUsername(authenticationHelper.getName());
		
		user.setLastVisit(new Date(System.currentTimeMillis()));
		userBo.update(user);
		
		sessionBean.setCustomerId(user.getCustomerId());
		
		sessionBean.setExecutor(user);
		
		return Action.SUCCESS;
	}
	
	public String execute() {
	      return Action.SUCCESS;
	}

	
	
	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public AuthenticationHelper getAuthenticationHelper() {
		return authenticationHelper;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public void setAuthenticationHelper(AuthenticationHelper authenticationHelper) {
		this.authenticationHelper = authenticationHelper;
	}
	
	public void setIterationBo(IterationBo iterationBo) {
		this.iterationBo = iterationBo;
	}

	public IterationBo getIterationBo() {
		return iterationBo;
	}
	
}